#!/usr/bin/perl

# File    : makeglossaries
# Author  : Nicola Talbot
# Version : 1.0 (2007/05/10)
# Description: simple Perl script that calls makeindex.
# Intended for use with "glossaries.sty" (saves having to remember
# all the various switches)

# This file is distributed as part of the glossaries LaTeX package.
# Copyright 2007 Nicola L.C. Talbot
# This work may be distributed and/or modified under the
# conditions of the LaTeX Project Public License, either version 1.3
# of this license of (at your option) any later version.
# The latest version of this license is in
#   http://www.latex-project.org/lppl.txt
# and version 1.3 or later is part of all distributions of LaTeX
# version 2005/12/01 or later.
#
# This work has the LPPL maintenance status `maintained'.
#
# The Current Maintainer of this work is Nicola Talbot.

# This work consists of the files glossaries.dtx and glossaries.ins 
# and the derived files glossaries.sty, glossary-hypernav.sty, 
# glossary-list.sty, glossary-long.sty, glossary-super.sty, 
# glossaries.perl. Also makeglossaries and makeglossaries.

use Getopt::Std;

if ($#ARGV < 0)
{
   die "Syntax : $0 [-ilqrcg] [-s sty] [-o gls] [-t log] [-p num] <filename>\n";
}

getopt('sotp');
getopts('ilqrcg');

# define known extensions

 %exttype = (
   main => {in=>'glo', out=>'gls', 'log'=>'glg'},
 );

$ext = '';

if (length(@ARGV[0]) < 4)
{
   $name = @ARGV[0];
}
elsif (substr(@ARGV[0],-4,1) eq ".")
{
  $name = substr(@ARGV[0],0,length(@ARGV[0])-4);

  $ext = substr(@ARGV[0],-3,3);
}
else
{
   $name = @ARGV[0];
}

$istfile = "$name.ist";

# check log file for other glossary types
# and for ist file name

if (open AUXFILE, "$name.aux")
{
   while (<AUXFILE>)
   {
      if (m/\\\@newglossary\s*\{(.*)\}{(.*)}{(.*)}{(.*)}/
        and ($1 ne 'main'))
      {
         $exttype{$1}{'log'} = $2;
         $exttype{$1}{'out'} = $3;
         $exttype{$1}{'in'}  = $4;

         if (!$opt_q)
         {
            print "added glossary type '$1' ($2,$3,$4)\n";
         }
      }

      if (m/\\\@istfilename\s*{([^}]*)}/)
      {
         $istfile = $1;
      }
   }

   close AUXFILE;
}
else
{
   print "unable to open $name.aux\n";
}

# save all the general makeindex switches

$mkidxopts = '';

if ($opt_i)
{
   $mkidxopts .= " -i";
}

if ($opt_l)
{
   $mkidxopts .= " -l";
}

if ($opt_q)
{
   $mkidxopts .= " -q";
}

if ($opt_r)
{
   $mkidxopts .= " -r";
}

if ($opt_c)
{
   $mkidxopts .= " -c";
}

if ($opt_g)
{
   $mkidxopts .= " -g";
}

unless ($opt_p eq "")
{
   $mkidxopts .= " -p $opt_p";
}

unless ($opt_s eq "")
{
   $istfile = $opt_s;
}

if ($ext ne '')
{
   %thistype = %{$exttype{'main'}}; #default

   foreach $type (keys %exttype)
   {
      if ($exttype{$type}{'in'} eq $ext)
      {
         %thistype = %{$exttype{$type}};

         last;
      }
   }

   if ($opt_o eq "")
   {
      $outfile = "$name.$thistype{out}";
   }
   else
   {
      $outfile = $opt_o;
   }

   if ($opt_t eq "")
   {
      $transcript = "$name.$thistype{'log'}";
   }
   else
   {
      $transcript = $opt_t;
   }

   &makeindex("$name.$ext",$outfile,$transcript,$istfile,
              $mkidxopts,$opt_q);
}
else
{
   foreach $type (keys %exttype)
   {
      %thistype = %{$exttype{$type}};

      $inputfile = "$name.$thistype{in}";

      if (-r $inputfile)
      {
         if ($opt_o eq "")
         {
            $outfile = "$name.$thistype{out}";
         }
         else
         {
            $outfile = $opt_o;
         }

         if ($opt_t eq "")
         {
            $transcript = "$name.$thistype{'log'}";
         }
         else
         {
           $transcript = $opt_t;
         }

         &makeindex($inputfile,$outfile,$transcript,
                    $istfile,$mkidxopts,$opt_q);
      }
      elsif (!$opt_q)
      {
         print "no read access for '$inputfile'\n";
      }
   }
}

sub makeindex{
   local($in,$out,$trans,$ist,$rest,$quiet) = @_;
   local($name,$cmdstr,$buffer,$n,$i,$j);
   local(@stuff,@item);

   $cmdstr = "$rest -s $ist -t $trans -o $out $in";

   unless ($quiet)
   {
      print "makeindex $cmdstr\n";
   }

   `makeindex $cmdstr`;
}

1;
